<template>
  <div class="login-container">
    <div class="logo">
      <img src="../../assets/images/logo.png" />
      <!--span>试用版</span-->
    </div>
    <el-form class="login-form" autoComplete="on" :model="loginForm" ref="loginForm" label-position="right" size="large">
      <div class="title-container">
        <h3 class="title">管理员登录</h3>
      </div>
      <el-form-item prop="userAccount" :rules="[rule[0]]">
        <el-input tabindex="1" name="userAccount" type="text" v-model="loginForm.userAccount" autoComplete="on" :rules="[rule[0],{ min: 5, message: '最少输入5个字符', trigger: 'blur'}]" placeholder="请输入帐号" prefix-icon="iconfont icon-account" />
      </el-form-item>

      <el-form-item prop="password" :rules="[rule[0]]">
        <el-input tabindex="2" name="password" :type="passwordType" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on" :rules="[rule[0],{ min: 5, message: '最少输入5个字符', trigger: 'blur'}]" placeholder="请输入密码" prefix-icon="iconfont icon-password" />
        <span class="show-pwd" @click="showPwd">
          <svg-icon icon-class="eye" />
        </span>
      </el-form-item>

      <el-form-item prop="code" :rules="[{ required: true, message: '必填项', trigger: 'blur' },{ min: 4, max: 4, message: '长度为  4 个字符', trigger: 'blur' }]">
        <el-input tabindex="3" :maxlength="4" placeholder="请输入验证码" @keyup.enter.native="handleLogin" v-model.trim="loginForm.code" prefix-icon="iconfont icon-ecurityCode">
          <template slot="append" v-if="loginForm.codeimg">
            <el-tooltip class="item" effect="dark" content="点击刷新" placement="left">
              <img :src="loginForm.codeimg" style="vertical-align: middle; height: 38px; display: block;" @click="refresh">
            </el-tooltip>
          </template>
        </el-input>
      </el-form-item>

      <el-button type="primary" style="width:100%;margin-bottom:30px;" :loading="loading" @click="handleLogin">登 录</el-button>
      <div class="tips">温馨提示：请注意帐号安全</div>
      <!--div class="tips" style="text-align: left;">试用说明：<br>1.该试用版仅作试用，不作商业用途; 2.该试用版对应的前台程序不是完整版; 3.如有商业用途请联系我们购买商业版; 4.经销商联系方式：王经理136-3600-1272</div-->
    </el-form>
  </div>
</template>

<script>
import { getCode } from '@/utils/api'
import uuid from 'uuid'
export default {
  name: 'login',
  data() {
    return {
      loginForm: {
        uuid: uuid(),
        userAccount: '',
        password: '',
        code: '',
        codeimg: null
      },
      rule: [
        { required: true, message: '必填项', trigger: 'change' }
      ],
      passwordType: 'password',
      loading: false
    }
  },
  created() {
    this.refresh()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    async refresh() {
      console.log(this)
      const data = await getCode({ uuid: this.loginForm.uuid, t: (new Date()).getTime() })
      console.log(data, 222)
      this.loginForm.codeimg = window.URL.createObjectURL(data)
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('LoginByUsername', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({ path: '/' })
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style>
.login-container .el-input-group__append {
  padding: 0;
}
</style>
<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;

.login-container {
  position: fixed;
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  background: url(../../assets/images/login.jpg) no-repeat;
  background-size: cover;

  .logo {
    display: flex;
    align-items: center;
    position: absolute;
    left: 0;
    top: 0;
    padding: 50px 0 0 50px;
    span {
      display: block;
      background: #ff0000;
      color: #fff;
      margin-left: 15px;
      font-size: 18px;
    }
  }
  .login-form {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    padding: 35px 50px;
    background: rgba(255, 255, 255, 1);
    border-radius: 6px;
    margin-top: 50px;
  }
  .tips {
    font-size: 14px;
    margin-bottom: 10px;
    text-align: center;
    color: $dark_gray;
  }
  .title-container {
    position: relative;
    .title {
      font-size: 24px;
      margin: 10px 0 30px 0;
      text-align: center;
      font-weight: normal;
      color: #97a8be;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 3px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 35px;
    bottom: 28px;
  }
}
</style>
